<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/10
 * Time: 22:34
 * 资金
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Commission;
use app\common\model\UserCoupon;
use app\common\model\Withdraw;
use think\Db;
use think\Exception;

class Funds extends Api {
	
	/**
	 * 资金记录
	 */
	public function index(){
		$userId = $this->auth->id;
		$model = new \app\common\model\Funds();
		$where['user_id'] = $userId;
		
		$operation_type = $this->request->post('operation_type');
		$operation_type && $where['operation_type'] = $operation_type;
		
		$type = $this->request->post('type');
		$type && $where['type'] = $type;
		
		$page = $this->request->post('page', 1, 'intval');
		$count = $model->where($where)->count();
		$list = $model->where($where)
			->page($page)
			->field('*, from_unixtime(create_time, "%Y-%m-%d") as date')
			->order(['create_time' => 'desc'])
			->select();
		$this->success('查询成功', [
			'total' => $count,
			'list' => $list
		]);
	}
	
	/**
	 * 佣金
	 */
	public function commission(){
		$userId = $this->auth->id;
		$model = new Commission();
		$where['user_id'] = $userId;
		
		
		$page = $this->request->post('page', 1, 'intval');
		$count = $model->where($where)->count();
		$list = $model->where($where)
			->page($page)
			->order(['create_time' => 'desc'])
			->select();
		$this->success('查询成功', [
			'total' => $count,
			'list' => $list
		]);
	}
	
	
	/**
	 * 提现
	 */
	public function withdraw() {
		$amount = $this->request->post('amount');
		if($amount < 1) {
			$this->error('最少提现1元');
		}
		
		$userId = $this->auth->id;
		$user = \app\admin\model\User::get($userId);
		if($user['money'] < $amount) {
			$this->error('余额不足');
		}
		
		$model = new Withdraw();
		$res = $model->withdraw($user, $amount);
		if($res) {
			$this->success('提交成功，等待转帐');
		} else {
			$this->error($model->getError());
		}
	}
	
	/**
	 * 提现记录
	 */
	public function withdrawRecord() {
		$userId = $this->auth->id;
		$model = new Withdraw();
		$where['user_id'] = $userId;
		
		
		$page = $this->request->post('page', 1, 'intval');
		$count = $model->where($where)->count();
		$list = $model->where($where)
			->page($page)
			->order(['create_time' => 'desc'])
			->field('*, from_unixtime(create_time, "%Y-%m-%d") as date')
			->select();
		$this->success('查询成功', [
			'total' => $count,
			'list' => $list
		]);
	}
	
	
	/**
	 * 分享的商品佣金
	 */
	public function shareCommission() {
		$goodsId = $this->request->post('goods_id');
		!$goodsId && $this->error('参数错误');
		$type = $this->request->post('type', 1, 'intval');
		
		$userId = $this->auth->id;
		if($type == 1) {
			$rows = Db::name('commission')->alias('c')
				->join('user u', 'c.relation_user_id = u.id', 'left')
				->where(['c.user_id' => $userId, 'c.goods_id' => $goodsId, 'c.status' => Commission::STATUS_Y])
				->field('u.nickname, c.amount, from_unixtime(c.update_time) as date')
				->select();
		} else if($type == 2) {
			$rows = Db::name('commission')->alias('c')
				->join('user u', 'c.relation_user_id = u.id', 'left')
				->where(['c.user_id' => $userId, 'c.goods_id' => $goodsId, 'c.status' => Commission::STATUS_WAIT])
				->field('u.nickname, c.amount, from_unixtime(c.create_time) as date')
				->select();
		} else {
			$rows = Db::name('orderGoods')->alias('og')
				->join('order o', 'og.order_id = o.id', 'left')
				->join('user u', 'o.user_id = u.id', 'left')
				->where(['o.status' => \app\common\model\Order::STATUS_WAIT, 'og.goods_id' => $goodsId, 'og.recommend_user_id' => $userId])
				->field('u.nickname, og.amount * og.number as amount, from_unixtime(o.create_time) as date')
				->select();
		}
		
		$this->success('查询成功', $rows);
	}
	
	/**
	 * 领取优惠券
	 */
	public function reveiceCoupon() {
		$id = $this->request->post('id');
		!$id && $this->error('请选择要领取的优惠券');
		
		$coupon = Db::name('coupon')->where(['id' => $id, 'status' => 1])->find();
		!$coupon && $this->error('优惠券不存在');
		
		$userId = $this->auth->id;
		$row = Db::name('userCoupon')->where(['user_id' => $userId, 'coupon_id' => $id])->find();
		$row && $this->error('你已经领取过了');
		
		$model = new UserCoupon();
		$model->startTrans();
		try {
			$couponRes = Db::name('coupon')->where(['id' => $id])->setInc('receive_number');
			if(!$couponRes) {
				throw new Exception('更改领取数量出错');
			}
			
			$res = $model->isUpdate(false)->save([
				'coupon_id' => $id,
				'user_id' => $userId,
				'name' => $coupon['name'],
				'min_amount' => $coupon['min_amount'],
				'amount' => $coupon['amount'],
				'desc' => $coupon['desc'],
				'expired_time' => time()+$coupon['expired_days']*24*3600
			]);
			
			if(!$res) {
				throw new Exception('领取出错');
			}
			$model->commit();
			$this->success('领取成功');
		} catch (Exception $e) {
			$model->rollback();
			$this->error($e->getMessage());
		}
	}
}
